<?php
  class StusurveyController extends Controller{
           public $isActivated = 2;
    public function accessRules()
		 {
			return array(
				array(
					'allow',  // allow all users to access 'index' and 'view' actions.
					'expression'=>'$user->rights == 1',
				),
				array(
					'deny',  // deny all users
					'users'=>array('*'),
					'message'=>'You do not have access to this page ! ! ',
				),
			);
		 }
      public function actionIndex(){
		     $this->breadcrumbs=array(
               '调查问卷'=>array('index'),
               '所有问卷',
             );
			  $ci = new CDbCriteria;
			  $ci->addCondition('ispost = 1');
			  $total = Survey::model()->count($ci);
			  $pages =  new CPagination($total);
			  $pages->pageSize = 20;
			  $pages->applyLimit($ci);
			 $surs = Survey::model()->findAll($ci);
			 $stusurs = Stusurvey::model()->findAll('uId = :u',array(':u'=>Yii::app()->user->getId()));
			 
	       $this->render("index",array('surs'=>$surs,'stusurs'=>$stusurs,'pages'=>$pages));	    
		  
	  }
	 public function actionMysurvey(){
			$this->breadcrumbs=array(
               '调查问卷'=>array('index'),
               '我的问卷',
             );	
 		      $ci = new CDbCriteria;
			  $ci->addCondition('`t`.uId = '.Yii::app()->user->getId());
			  $total = Stusurvey::model()->count($ci);
			  $pages =  new CPagination($total);
			  $pages->pageSize = 1;
			  $pages->applyLimit($ci);
			  $stusurs = Stusurvey::model()->findAll($ci);
			  $this->render("mysurvey",array('stusurs'=>$stusurs,'pages'=>$pages));	    
		  
	  }
	   
	 public function actionSurvey(){
		   $request = Yii::app()->request;
		   $sur = Survey::model()->findByPk($request->getParam('surveyId'));
		   if($sur){
			   $stusur = Stusurvey::model()->find('surveyId = :s and uId = :u',array(':s'=>$sur->surveyId,':u'=>Yii::app()->user->getId()));
			   //重复答题限制
			   if($stusur){
				   
				   Yii::app()->user->setFlash('addsurvey','不能重复答题');
				   $this->redirect(array('stusurvey/index'));
				   
			   }
			   $qus = Question::model()->findAll('surveyId = :s',array(':s'=>$sur->surveyId));
			   
		   }else{
			   $this->redirect(array('stusurvey/index'));
		   }
	       $this->render("survey",array('sur'=>$sur,'qus'=>$qus));	    
		  
	  }
	  
    public function actionAddsurvey(){
		$request = Yii::app()->request;
		if($request->requestType == 'POST'){
			$sur = Survey::model()->findByPk($request->getParam('surveyId'));
			$stusur = Stusurvey::model()->find('surveyId = :s and uId = :u',array(':s' =>$request->getParam('surveyId'),':u'=>Yii::app()->user->getId()));
			if($sur && !$stusur){
				$qus = array();
				$qusTemp = Question::model()->findAll('surveyId = :s',array(':s'=>$sur->surveyId));
				foreach($qusTemp as $q){
					
					$qus[$q->questionId] = $q;
				}
				$answer = $request->getParam('answer');
				/*
				   The post data
				   
					array(2) {
					  ["surveyId"]=>
					  string(1) "6"
					  ["answer"]=>
					  array(3) {
						[7]=>
						array(1) {
						  [0]=>
						  string(1) "2"
						}
						[8]=>
						array(2) {
						  [0]=>
						  string(1) "3"
						  [1]=>
						  string(1) "4"
						}
						[9]=>
						array(1) {
						  [0]=>
						  string(24) "吃速度才收到城市"
						}
					  }
					}

				
				*/
				
				if(count($qus) == count($answer)){
					
					foreach($answer as $key => $a){
						// 根据 questionId 来处理答案
						switch($qus[$key]->type){
							case 'danxuan': //
											$sta = json_decode($qus[$key]->statistic);
											$sta->$a[0] += 1;
											$qus[$key]->statistic = json_encode($sta);
											$qus[$key]->save();
											break;
							case 'duoxuan': //
											$sta = json_decode($qus[$key]->statistic);
											foreach($a as $item){
											   $sta->$item += 1;	
											}
											$qus[$key]->statistic = json_encode($sta);
											$qus[$key]->save();
											break;
							case 'tiankong'://
											$blankfillItem = new Blankfillitem;
											$blankfillItem->questionId = $qus[$key]->questionId;
											$blankfillItem->surveyId = $sur->surveyId;
											$blankfillItem->content = $a[0];
											$blankfillItem->save();
											break;
							
						}
						
					}
					$stusur = new Stusurvey;
					
					$stusur->uId = Yii::app()->user->getId();
					$stusur->surveyId = $sur->surveyId;
					$stusur->addtime = date('Y-m-d H:i:s');
					$stusur->history = json_encode($answer);
					
					$sur->attendnum += 1;
					
					if($sur->save()){
						if($stusur->save()){
							Yii::app()->user->setFlash('addsurvey','谢谢您的参与,问卷添加成功');
					    }else{
							$this->logAr($stusur);
							Yii::app()->user->setFlash('addsurvey','谢谢您的参与,问卷添加成功');
						}
							
					}else{
						$this->logAr($sur);
						Yii::app()->user->setFlash('addsurvey','谢谢您的参与,问卷添加失败');
					}
					
				}// if params equal
				
			}// if the sur exist and there is no stusur of this survey
			
			
		}
			
			$this->redirect(array('stusurvey/index'));
			
		
	}	  
  
  }
  
  
?>